using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using SoulDoorEngine.Framework;
using SoulDoorEngine.Framework.Components;
using SoulDoorEngine.Components.Smart2D.Objects;
using SoulDoorEngine.Components.Smart2D.Basic;
using SoulDoorEngine.Components.Smart2D.Enums;
using SoulDoorEngineSmart.Components.Audio;

namespace TestSmart2D
{
    /// <summary>
    /// 这是游戏的主类型
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

        GraphicObj q;
        GraphicObj c;
        GraphicObj j;
        GraphicObj Up;
        GraphicObj Left;
        GraphicObj Right;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = 480;
            graphics.PreferredBackBufferHeight = 800;
            //添加横屏和竖屏的支持
            graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;  
            
            // Windows Phone 的默认帧速率为 30 fps。
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // 延长锁定时的电池寿命。
            InactiveSleepTime = TimeSpan.FromSeconds(1);
        }

        /// <summary>
        /// 允许游戏在开始运行之前执行其所需的任何初始化。
        /// 游戏能够在此时查询任何所需服务并加载任何非图形
        /// 相关的内容。调用 base.Initialize 将枚举所有组件
        /// 并对其进行初始化。 
        /// </summary>
        protected override void Initialize()
        {
            // TODO: 在此处添加初始化逻辑
            this.graphics.IsFullScreen = true;

            base.Initialize();
        }

        /// <summary>
        /// 对于每个游戏会调用一次 LoadContent，
        /// 用于加载所有内容。
        /// </summary>
        protected override void LoadContent()
        {
            // 创建新的 SpriteBatch，可将其用于绘制纹理。
            Engine.SetupEngine(graphics);
            AudioPlayer.SetupAudioPlayer(this);

            // TODO: 在此处使用 this.Content 加载游戏内容
            q = new GraphicObj(Vector.Zero, Vector.Zero, Vector.Zero, "前景2", this, new Point(800, 480), new Point(1, 1), Point.Zero, 10, this, CollisionDetectionLevels.PerPixelCollision, MouseButtonDownDetectionLevels.None);
            c = new GraphicObj(Vector.Zero, Vector.Zero, Vector.Zero, "彩景", this, new Point(800, 480), new Point(1, 1), Point.Zero, 10, this, CollisionDetectionLevels.None, MouseButtonDownDetectionLevels.None);
            j = new GraphicObj(Vector.Zero, new Vector(5.4f,0), Vector.Zero, "角色", this, new Point(40, 50), new Point(22, 1), Point.Zero, 2, this, CollisionDetectionLevels.PerPixelCollision, MouseButtonDownDetectionLevels.None, true);

            Up = new GraphicObj(new Vector(665, 365), Vector.Zero, Vector.Zero, "Up", this, new Point(100, 100), new Point(1, 1), Point.Zero, 10, this, CollisionDetectionLevels.None, MouseButtonDownDetectionLevels.RectangleCollision);
            Left = new GraphicObj(new Vector(27, 365), Vector.Zero, Vector.Zero, "Left", this, new Point(100, 100), new Point(1, 1), Point.Zero, 10, this, CollisionDetectionLevels.None, MouseButtonDownDetectionLevels.RectangleCollision);
            Right = new GraphicObj(new Vector(141, 365), Vector.Zero, Vector.Zero, "Right", this, new Point(100, 100), new Point(1, 1), Point.Zero, 10, this, CollisionDetectionLevels.None, MouseButtonDownDetectionLevels.RectangleCollision);
            

            q.LayerDepth = 0.4f;
            c.LayerDepth = 0.5f;
            j.LayerDepth = 0.1f;

            j.CollisionObjList.Add(q);
            j.Collision += new GraphicObj.CollisionEventHandler(j_Collision);

            Up.MouseLeftButtonDown += new GraphicObj.MouseLeftButtonDownEventHandler(Up_MouseLeftButtonDown);
            Left.MouseLeftButtonDown += new GraphicObj.MouseLeftButtonDownEventHandler(Left_MouseLeftButtonDown);
            Right.MouseLeftButtonDown += new GraphicObj.MouseLeftButtonDownEventHandler(Right_MouseLeftButtonDown);

            AudioPlayer.AddSound("Sound1");
            AudioPlayer.AddSong("Song");
        }

        void Right_MouseLeftButtonDown(object sender, SoulDoorEngine.Components.Smart2D.Events.MouseButtonDownEventArgs e)
        {
            j.Velocity += new Vector(5f, 0f);
        }

        void Left_MouseLeftButtonDown(object sender, SoulDoorEngine.Components.Smart2D.Events.MouseButtonDownEventArgs e)
        {
            j.Velocity += new Vector(-5f, 0f);
        }

        void Up_MouseLeftButtonDown(object sender, SoulDoorEngine.Components.Smart2D.Events.MouseButtonDownEventArgs e)
        {
            j.Velocity += new Vector(0f, -10f);
        }

        void j_Collision(object sender, SoulDoorEngine.Components.Smart2D.Events.CollisionEventArgs e)
        {
            GraphicObj go = (GraphicObj)sender;
            j.Physics.OnGround = true;
            if (j.Velocity.X >= 0.2f && j.Velocity.Y >= 0.2f || j.Velocity.Y >= 1f)
            {
                AudioPlayer.PlaySound("Sound1");
            }
            //j.Acceleration = Vector.Zero;
            //j.Velocity = new Vector(j.Velocity.X, 0);
        }

        /// <summary>
        /// 对于每个游戏会调用一次 UnloadContent，
        /// 用于取消加载所有内容。
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: 在此处取消加载任何非 ContentManager 内容
        }

        /// <summary>
        /// 允许游戏运行逻辑，例如更新全部内容、
        /// 检查冲突、收集输入信息以及播放音频。
        /// </summary>
        /// <param name="gameTime">提供计时值的快照。</param>
        protected override void Update(GameTime gameTime)
        {
            // 允许游戏退出
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: 在此处添加更新逻辑
            Engine.Update(gameTime);

            AudioPlayer.PlaySong("Song");

            base.Update(gameTime);
        }

        /// <summary>
        /// 当游戏该进行自我绘制时调用此项。
        /// </summary>
        /// <param name="gameTime">提供计时值的快照。</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: 在此处添加绘图代码
            Engine.Draw(gameTime, ComponentType.All);

            base.Draw(gameTime);
        }
    }
}
